package org.dbstar.scheduler.actions;

import org.dbstar.scheduler.beans.Catalog;
import org.dbstar.scheduler.beans.OperateType;
import org.dbstar.scheduler.control.CatalogController;
import org.dbstar.scheduler.control.exception.DaoException;


/**
 * 控制Catalog的增删改查
 * 
 * @author 代波
 * @since 1.0.0
 * @version 1.0.0
 * @date 2012-4-23
 */
public class CatalogsAction extends ActionSupport {
	private CatalogController catalogController;

	private String change;
	private String message;
	private int id;
	private int parent;
	private String name;
	private String description;

	public CatalogController getCatalogController() {
		return catalogController;
	}

	public void setCatalogController(CatalogController catalogController) {
		this.catalogController = catalogController;
	}

	public int getId() {
		return id;
	}

	public void setId(int id) {
		this.id = id;
	}

	public int getParent() {
		return parent;
	}

	public void setParent(int parent) {
		this.parent = parent;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getDescription() {
		return description;
	}

	public void setDescription(String description) {
		this.description = description;
	}

	public void setChange(String change) {
		this.change = change;
	}

	public String getMessage() {
		return message;
	}

	@Override
	public String execute() throws Exception {
		if ("add".equals(change)) {
			Catalog cataLog = new Catalog();
			cataLog.setParent(id);
			cataLog.setName(name);
			cataLog.setDescription(description);
			cataLog.setOwner(user);
			try {
				int newId = catalogController.addCatalog(cataLog);
				message = "增加新目录成功，Catalog ID=" + newId;
				operate(OperateType.catalog_add, String.valueOf(newId));
			} catch (DaoException e) {
				message = "增加新目录失败：" + e.getLocalizedMessage();
			}
		} else if ("update".equals(change)) {
			Catalog cataLog = catalogController.getCatalog(id);
			if (cataLog == null) {
				message = "更新目录失败：目录不存在：" + id;
			} else {
				cataLog.setName(name);
				cataLog.setDescription(description);
				cataLog.setModifier(user);
				try {
					catalogController.updateCatalog(cataLog);
					message = "更新目录成功，Catalog ID=" + id;
					operate(OperateType.catalog_update, String.valueOf(id));
				} catch (DaoException e) {
					message = "更新目录失败：" + e.getLocalizedMessage();
				}
			}
		} else if ("move".equals(change)) {
			try {
				catalogController.moveCatalog(id, parent);
				message = "移动目录成功，Catalog ID=" + id;
				operate(OperateType.catalog_move, String.valueOf(id));
			} catch (DaoException e) {
				message = "移动目录失败：" + e.getLocalizedMessage();
			}
		} else if ("remove".equals(change)) {
			try {
				catalogController.removeCatalog(id);
				message = "删除目录成功，Catalog ID=" + id;
				operate(OperateType.catalog_remove, String.valueOf(id));
			} catch (DaoException e) {
				message = "删除目录失败：" + e.getLocalizedMessage();
			}
		} else {
			operate(OperateType.catalogs_list, (String) null);
		}
		return SUCCESS;
	}
}
